package adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a17376080_dlz_finaltest.R;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import utils.HttpUtil;

public class WorldAdapter extends RecyclerView.Adapter<WorldAdapter.ViewHolder>{
    private Context mContext;
    private static final int SUCCESS = 1;
    private List<World> mworldList;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                //加载网络成功进行UI的更新,处理得到的图片资源
                case SUCCESS:
                    Object[] objects= (Object[]) msg.obj;
                   // byte[] Picture = (byte[]) objects[1];
                    ImageView imageView= (ImageView) objects[0];
                    if(imageView.getTag().equals(objects[2])){
                        byte[] Picture = (byte[]) objects[1];
                        //使用BitmapFactory工厂，把字节数组转化为bitmap
                        Bitmap bitmap = BitmapFactory.decodeByteArray(Picture, 0, Picture.length);
                        //通过imageview，设置图片
                        imageView.setImageBitmap(bitmap);
                    }
                    break;
                default:
                    break;


            }
        }
    };
    static class ViewHolder extends RecyclerView.ViewHolder{
       ImageView image;
        TextView title,author,fabulous;
        View worldView;
        public ViewHolder(View view){
            super(view);
            worldView=view;
            title=view.findViewById(R.id.tv_world_title);
            author=view.findViewById(R.id.tv_world_anthon);
            image=view.findViewById(R.id.iv_world_bg);

        }
    }
    public WorldAdapter(List<World> comicList,Context context){
        this.mworldList=comicList;
        this.mContext=context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.world_item,parent,false);
        final ViewHolder holder=new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        World world=mworldList.get(position);
        holder.title.setText(world.getRe_content());
        holder.image.setImageDrawable(mContext.getDrawable(R.mipmap.timg));
        holder.image.setTag(world.getRe_image());
        sendImageViewHttp(world.getRe_image(),holder.image);
        holder.author.setText(world.getRe_username());

    }
    private void sendImageViewHttp(final String image ,final ImageView imageView) {
        HttpUtil.sendOkHttpRequest(image, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("失败了", "onFailure: ");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                byte[] Picture_bt = response.body().bytes();
                //通过handler更新UI
                Object[] objects=new Object[]{imageView,Picture_bt,image};
                Message message=handler.obtainMessage();
                message.obj=objects;
                message.what = SUCCESS;
                handler.sendMessage(message);

            }
        });
    }
    @Override
    public int getItemCount() {
        return mworldList.size();
    }
}
